package graduation.controller;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import graduation.entity.TeamActivity;
import graduation.service.TeamActivityService;
import graduation.utils.FileUtilt;
import graduation.utils.ResultVOUtil;
import graduation.vo.PageVO;
import graduation.vo.ResultVO;

@Controller
@RequestMapping("/teamActivity")
public class TeamActivityController {
	@Autowired
	private TeamActivityService teamActivityServiceImpl;
	
	@RequestMapping(value="addTeamActivity",method=RequestMethod.POST)//用来处理请求地址映射的注解
	@ResponseBody//@Responsebody 注解表示该方法的返回的结果直接写入 HTTP 响应正文（ResponseBody）中
	public ResultVO addTeamActivity(@RequestParam("title") String title,@RequestParam("info") String info,@RequestParam("cover") MultipartFile cover,String outLine,HttpServletRequest request

			){
	
		TeamActivity teamActivity = new TeamActivity();
		
		//文件原始名称（上传前的名字）
		String originalFileName = cover.getOriginalFilename();
		//图片到服务器的名称
		String urlFileName = String.valueOf(new Date().getTime())+originalFileName;
		//获取文件类型
		String contentType = cover.getContentType();
		
		//前半部分路径，目录，将WebRoot下的一个名称为Files文件夹转换成绝对路径
		String leftPath = request.getSession().getServletContext().getRealPath("/Files/");
		System.out.println("文件类型"+contentType);
		/*
		 * 本地的映射资源
		 * /attachments/**
		 */
		//文件上传
		try {
			FileUtilt.uploadFile(cover.getBytes(), leftPath, urlFileName);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//图片的地址
		String photoUrl = "graduation"+"/attachments/"+urlFileName;
		//保存到数据库
		//获取随机不重复id
		String userId = UUID.randomUUID().toString();
		userId = userId.replace("-", "");
		teamActivity.setId(userId);
		teamActivity.setCover(photoUrl);
		teamActivity.setTitle(title);
		teamActivity.setInfo(info);
		teamActivity.setOutLine(outLine);
		//保存到数据库
		teamActivityServiceImpl.addTeamActivity(teamActivity);
		return ResultVOUtil.success(teamActivity);
		
	}
	/**
	 * 修改
	 * @param teamActivityId
	 * @param title
	 * @param info
	 * @param cover
	 * @param date
	 * @return
	 */
	@RequestMapping(value="updateTeamActivity",method=RequestMethod.POST)
	@ResponseBody
	public ResultVO updateTeamActivity(@RequestParam("teamActivityId") String teamActivityId,
			@RequestParam("title") String title,
			@RequestParam("info") String info,
			@RequestParam(name="cover",required=false) MultipartFile cover,
			@RequestParam("outline") String outline,
			HttpServletRequest request){
	
		//通过id查询数据库中是否有该数据
		TeamActivity teamActivity = teamActivityServiceImpl.getActivityById(teamActivityId);
		if(teamActivity==null){
			return ResultVOUtil.fail("该用户不存在");
		}
		if(cover != null){
			//文件原始名称（上传前的名字）
			String originalFileName = cover.getOriginalFilename();
			//图片到服务器的名称
			String urlFileName = String.valueOf(new Date().getTime())+originalFileName;
			//前半部分路径，目录，将WebRoot下的一个名称为Files文件夹转换成绝对路径
			String leftPath = request.getSession().getServletContext().getRealPath("/Files/");
//			//获取文件类型
//			String contentType = cover.getContentType();
//			
//			//判断是否为图片(如果不包含必定全部返回值为-1)
//					if(contentType.indexOf("image") == -1){
//						return ResultVOUtil.fail("图片格式错误");
//					}
//			
			try {
				//上傳
				FileUtilt.uploadFile(cover.getBytes(), leftPath, urlFileName);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//图片的地址
			String photoUrl = "graduation"+"/attachments/"+urlFileName;
			teamActivity.setCover(photoUrl);
		}
		
		//保存到数据库
		
		teamActivity.setDate(new Date());
		teamActivity.setOutLine(outline);
		teamActivity.setTitle(title);
		teamActivity.setInfo(info);
		//修改
		teamActivityServiceImpl.updateActivityById(teamActivity);
		return ResultVOUtil.success(teamActivity);
		
	}
	/**
	 * 根据id删除
	 * @param teamActivityId
	 * @return
	 */
	@RequestMapping(value="delActivity",method=RequestMethod.POST)
	@ResponseBody
	public ResultVO delActivity(@RequestBody Map<String,Object> map,HttpServletRequest request){
		String teamActivityId = map.get("teamActivityId").toString();
		TeamActivity teamActivity = teamActivityServiceImpl.getActivityById(teamActivityId);
		//前半部分路径，目录，将WebRoot下的一个名称为Files文件夹转换成绝对路径
		String leftPath = request.getSession().getServletContext().getRealPath("/Files/");
		//把磁盘上的文件删除
				try {
					FileUtilt.DeleteFile(leftPath, teamActivity.getCover().substring(teamActivity.getCover().lastIndexOf("/")+1));
					System.out.println("删除成功");
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		teamActivityServiceImpl.deleteActivity(teamActivity);
		return ResultVOUtil.success("删除成功");
		
	}
	/**
	 * 根据id获取具体信息
	 * @param teamActivityId
	 * @return
	 */
	@RequestMapping(value="getActivityById",method=RequestMethod.GET)
	@ResponseBody
	public ResultVO getActivityById(@RequestParam("teamActivityId") String teamActivityId){
		
		TeamActivity TeamActivity = teamActivityServiceImpl.getActivityById(teamActivityId);
		return ResultVOUtil.success(TeamActivity);
	}
	
	/**
	 * 根据最新时间获取内容
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="getAllActivity",method=RequestMethod.GET)
	@ResponseBody
	public ResultVO getAllActivity(@RequestParam("page") int page,@RequestParam("size") int size){
		
		PageVO pageVO = teamActivityServiceImpl.getAllActivity(page, size);

		return ResultVOUtil.success(pageVO);
		
	}
}
